package pricelist;


import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Vector;
import sort.Sortinterface;
import utility.CapitalChar;


public class Sortby  
{
	
	String buffer[];
	String FILENAME ="C:\\Documents and Settings\\osc1\\Desktop\\07_09_07\\itemWith_Grade_A.csv";
	//private String inputpath;
	//String FILENAME ="//home//osc//Desktop//Item List//Item//sortby.txt"; //input file
	String OUTPUTFILE = "C:\\Documents and Settings\\osc1\\Desktop\\out.csv";   
	
	public Sortby()
	{
		System.out.println("i m in sortby method");
		getPath();
		
	}
	public void addReport(Vector<Object> tempmap,String order) 
	{	
		try 
		{
			//String classname ="Descending";
			String classname =order;
			System.out.println("order of sorting-----"+classname);
			System.out.println("index of sorting-----"+tempmap);
			System.out.println("input file for sorting------"+FILENAME);
			
			Sortinterface sort;
			Class c = Class.forName("sort."+CapitalChar.makeFirstCharCapital(classname));
			sort = (Sortinterface) c.newInstance();
			sort.setbuffer(buffer);
		//	sort.setVector(tempmap);
			sort.getResult();
			buffer = sort.getbuffer();
			File file =new File(FILENAME); 
			RandomAccessFile raf =new RandomAccessFile(file,"rws");
			//RandomAccessFile raf =new RandomAccessFile(OUTPUTFILE,"rws");
			//System.out.println("after sorting");
			for(int i=0;i<buffer.length;i++)
			{
				raf.writeBytes(buffer[i]+"\n");
				System.out.println(buffer[i]);
			}
		}
		catch (ClassNotFoundException e) 
		{
			e.printStackTrace();
		}
		catch (InstantiationException e) 
		{
			e.printStackTrace();
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
		}
	}

	public void initializeData() 
	{	
		try
		{
			System.out.println(FILENAME);
			File file =new File(FILENAME); 
			RandomAccessFile raf =new RandomAccessFile(file,"r");
			String result="";
			StringBuffer tembuffer =new StringBuffer();
			while((result=raf.readLine())!=null)
			{
				tembuffer.append(result+"\n");
			}
			System.out.println("buffer before sorting"+tembuffer);
			buffer =tembuffer.toString().split("\n");
			file.delete();
			file.createNewFile();
		} 
		catch (FileNotFoundException e) 
		{
			e.printStackTrace();
		}
		catch (IOException e) 
		{
			e.printStackTrace();
		}
	}
	public void SetPath(String path)
	{
		this.FILENAME=path;		
	}
	public String getPath()
	{
		return FILENAME;		
	
	}
	

	public static void main(String[] args) 
	{
		Sortby sb = new Sortby();
		Vector<Object> temp =new Vector<Object>();
		String order="Ascending";
		temp.add(4);
		//temp.add(5);
		//temp.add(6);
		temp.add(1);
		//temp.add(2);
		sb.initializeData();
		sb.addReport(temp,order);
		
	}
}
